<?php
// $Id: sign.php, v 1.12 2005/02/05 C. Felix AKA the Cat
//  ------------------------------------------------------------------------ //
//             XF Guestbook                                                  //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //

include("../../mainfile.php");
if ($xoopsModuleConfig['anonsign']!=1 && !is_object($xoopsUser)) {
	redirect_header(XOOPS_URL."/user.php",2,_MD_XFGB_MUSTREGFIRST);
	exit();
}
include_once(XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/class/msg.php");
include_once(XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/include/functions.php");
include_once("include/config.inc.php");

$option = getOptions();
$msg_handler =& xoops_getmodulehandler('msg');

$messagesent = _MD_XFGB_MESSAGESENT;

$op = 'form';
foreach ( $_POST as $k => $v ) { ${$k} = $v; }
foreach ( $_GET as $k => $v ) { ${$k} = $v; }

if ( isset($preview)) {
	$op = 'preview';
} elseif ( isset($post) ) {
	$op = 'post';
}

function userCheck($email) {
	$msgstop = '';
	if ( $email !='' && !checkEmail($email) ) {
		$msgstop .= _MD_XFGB_INVALIDMAIL.'<br />';
	}
	return $msgstop;
}

function xfgb_upload() {
	global $xoopsModule, $xoopsModuleConfig, $preview_name, $msgstop;
	$created = time();
	$ext = preg_replace( "/^.+\.([^.]+)$/sU" , "\\1" , $_FILES['photo']['name']) ;
	include_once(XOOPS_ROOT_PATH."/class/uploader.php");
	$field = $_POST["xoops_upload_file"][0] ;
	if( !empty( $field ) || $field != "" ) {
		// Check if file uploaded
		if( $_FILES[$field]['tmp_name'] == "" || ! is_readable( $_FILES[$field]['tmp_name'] ) ) {
			$msgstop .= sprintf(_MD_XFGB_FILEERROR, $xoopsModuleConfig['photo_maxsize']);
		} else {
			$photos_dir = XOOPS_ROOT_PATH . "/modules/".$xoopsModule->dirname()."/images/msg" ;
			$array_allowed_mimetypes = array("image/gif","image/pjpeg","image/jpeg","image/x-png") ;
			$uploader = new XoopsMediaUploader( $photos_dir , $array_allowed_mimetypes , $xoopsModuleConfig['photo_maxsize'] ,  $xoopsModuleConfig['photo_maxwidth'] ,  $xoopsModuleConfig['photo_maxheight'] ) ;
			if( $uploader->fetchMedia( $field ) && $uploader->upload() ) {
				@unlink("$photos_dir/".$preview_name);
				$tmp_name = $uploader->getSavedFileName() ;
				$ext = preg_replace( "/^.+\.([^.]+)$/sU" , "\\1" , $tmp_name ) ;
				$preview_name = "tmp_".$created.".".$ext;
				rename( "$photos_dir/$tmp_name" , "$photos_dir/$preview_name" ) ;
			} else {
				$msgstop.= $uploader->getErrors();
			}
		}
	}
}

switch ($op) {
case "cancel":
	$photos_dir = XOOPS_ROOT_PATH . "/modules/".$xoopsModule->dirname()."/images/msg" ;
	$nb_removed_tmp = xfgb_clear_tmp_files( $photos_dir ) ;
	redirect_header("index.php", 0);
	break;
	
case "preview":
	$ts =& MyTextSanitizer::getInstance(); 

	$msgstop = '';
	if (!checkEmail($email) ) $msgstop .= _MD_XFGB_INVALIDMAIL.'<br />';
	if ( !empty($_FILES['photo']['name'])) {
		xfgb_upload();
	} 
	if ( !empty($msgstop) ) {
		include  XOOPS_ROOT_PATH.'/header.php';
		$xoopsOption['template_main'] = 'xfguestbook_signform.html';
		$xoopsTpl->assign('preview', true);
		$xoopsTpl->assign('msgstop',$msgstop);
		$xoopsTpl->assign('msg', $msg);
		include_once "include/form_sign.inc.php";
		$signform->assign($xoopsTpl);	
		include XOOPS_ROOT_PATH."/footer.php";
		exit();
	}
	$msgpost['poster'] = $name;
	$msgpost['user_id'] = $user_id;
	$msgpost['title'] = $ts->htmlSpecialChars($title);
	$msgpost['message'] = $ts->displayTarea($message);
	$msgpost['date'] = formatTimestamp(time(),"s");
	$msgpost['photo'] = $preview_name;
	if ($gender) 
		$msgpost['gender'] = '<img src="images/'.$gender.'.gif"';
	if ($email) 
		$msgpost['email'] = "<img src=\"".XOOPS_URL."/images/icons/email.gif\" alt=\""._SENDEMAILTO."\" />";
	if ($url) 
		$msgpost['url'] = '<img src="'.XOOPS_URL.'/images/icons/www.gif" alt="'._VISITWEBSITE.'">';
	if (isset($country)) {
		$flag = XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/images/flags/".$country.".gif";
		$arr_country = xfgb_getCountry("country_code ='".$country."'");	
		$country_name = (count($arr_country) > 0) ? $arr_country[0]['country_name'] : '';
		if (file_exists($flag)) {
			$msgpost['country'] = "<img src=\"".XOOPS_URL."/modules/xfguestbook/images/flags/".$country.".gif\" alt=\"".$country_name."\">";
		} else {
			$msgpost['country'] = $country_name;
		}
	}
	include  XOOPS_ROOT_PATH.'/header.php';
	$xoopsOption['template_main'] = 'xfguestbook_signform.html';
	$xoopsTpl->assign('preview', true);
	$msgstop = userCheck($email);
	$xoopsTpl->assign('msgstop',$msgstop);
	include "include/form_sign.inc.php";
	$xoopsTpl->assign('msg', $msgpost);
	$signform->assign($xoopsTpl);	
	include XOOPS_ROOT_PATH."/footer.php";

	break;

case "post":
	$msgstop = '';
	if (!checkEmail($email) ) $msgstop .= _MD_XFGB_INVALIDMAIL.'<br />';
	if ( !empty($_FILES['photo']['name'])) {
		xfgb_upload();
	} 
	if ( !empty($msgstop) ) {
		include  XOOPS_ROOT_PATH.'/header.php';
		$xoopsOption['template_main'] = 'xfguestbook_signform.html';
		$xoopsTpl->assign('preview', true);
		$xoopsTpl->assign('msgstop',$msgstop);
		$xoopsTpl->assign('msg', $msg);
		include_once "include/form_sign.inc.php";
		$signform->assign($xoopsTpl);	
		include XOOPS_ROOT_PATH."/footer.php";
		exit();
	}
	if ($preview_name != '') {
		$photo = str_replace('tmp_', 'msg_',$preview_name);
		$photos_dir = XOOPS_ROOT_PATH . "/modules/".$xoopsModule->dirname()."/images/msg" ;
		rename( "$photos_dir/$preview_name" , "$photos_dir/$photo" ) ;
	}

	$msgpost = $msg_handler->create();
	($xoopsUser) ? $user_id = $xoopsUser->uid() : $user_id=0;
	($xoopsUser) ? $username=$xoopsUser->uname() : $username=$name;
	$msgpost->setVar('user_id', $user_id);
	$msgpost->setVar('uname', $username);
	$msgpost->setVar('title', $title);
	$msgpost->setVar('message', $message);
	$msgpost->setVar('note', '');
	$msgpost->setVar('post_time', time());
	$msgpost->setVar('email', $email);
	$msgpost->setVar('url', $url);
	$msgpost->setVar('poster_ip', $_SERVER['REMOTE_ADDR']);
	$msgpost->setVar('moderate', $xoopsModuleConfig['moderate']);
	if (!isset($country)) $country='';
	$msgpost->setVar('country', $country);
	$msgpost->setVar('gender', $gender);
	$msgpost->setVar('photo', $photo);

	$nb_removed_tmp = xfgb_clear_tmp_files( $photos_dir ) ;
	
		if ($msg_handler->insert($msgpost)) {
			if($xoopsModuleConfig['moderate']) 
				$messagesent.="<br />"._MD_XFGB_AFTERMODERATE;

			// Send mail to webmaster
			if($xoopsModuleConfig['sendmail2wm']==1) {
				$subject = $xoopsConfig['sitename']." - "._MD_XFGB_NAMEMODULE;
				$xoopsMailer =& getMailer();
				$xoopsMailer->useMail();
				$xoopsMailer->setToEmails($xoopsConfig['adminmail']);
				$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
				$xoopsMailer->setFromName($xoopsConfig['sitename']);
				$xoopsMailer->setSubject($subject);
				$xoopsMailer->setBody(_MD_XFGB_NEWMESSAGE." ".XOOPS_URL."/modules/".$xoopsModule->dirname()."/");
				$xoopsMailer->send();
			}
			redirect_header("index.php",2,$messagesent);
		} else {
			$messagesent = $msgpost->getHtmlErrors();
			redirect_header("index.php",9,$messagesent);
		}
	break;
	
case 'form':
default:
	include XOOPS_ROOT_PATH."/header.php";
	$xoopsOption['template_main'] = 'xfguestbook_signform.html';
		
	include XOOPS_ROOT_PATH."/header.php";
	$user_id = !empty($xoopsUser) ? $xoopsUser->getVar("uid", "E") : 0;
	$name = !empty($xoopsUser) ? $xoopsUser->getVar("uname", "E") : "";
	$email = !empty($xoopsUser) ? $xoopsUser->getVar("email", "E") : "";
	$url = !empty($xoopsUser) ? $xoopsUser->getVar("url", "E") : "";
	$title = "";
	$message = "";
	$gender='';
	$preview_name='';
	$country = $option['opt_country'];

	include "include/form_sign.inc.php";
	$signform->assign($xoopsTpl);	
	include XOOPS_ROOT_PATH."/footer.php";
	break;
}
?>